%% Author: xiaomai
%% Created: 2010-11-17
%% Description: 诛邪系统协议信息的组包和解包
-module(pt_28).

%%
%% Include files
%%
-include("common.hrl").
-include("record.hrl").
%%
%% Exported Functions
%%
-export([write/2, read/2]).

%%
%% API Functions
%%

%%%=========================================================================
%%% 解包函数
%%%=========================================================================
%% -----------------------------------------------------------------
%% 28001 获取全服开宝箱记录
%% -----------------------------------------------------------------
read(28001, _R) ->	
	{ok, []};

%% -----------------------------------------------------------------
%% 28002 开宝箱
%% -----------------------------------------------------------------
read(28002, <<HoleType:16, OpenType:16>>) ->
  {ok, [HoleType, OpenType]};

%% -----------------------------------------------------------------
%% 28003 获取诛邪仓库数据
%% -----------------------------------------------------------------
read(28003, _R) ->
	{ok, []};

%% -----------------------------------------------------------------
%% 28004 将物品放入背包
%% -----------------------------------------------------------------
read(28004, <<GoodsId:32,GoodsNum:16, GoodsIndex:16>>) ->
	{ok, [GoodsId, GoodsNum, GoodsIndex]};

%% -----------------------------------------------------------------
%% 28005 将物品丢弃
%% -----------------------------------------------------------------
read(28005, <<GoodsId:32, GoodsNum:16>>) ->
	{ok, [GoodsId, GoodsNum]};

%% -----------------------------------------------------------------
%% 28006 丢弃仓库中的所有物品
%% -----------------------------------------------------------------

read(28006, _R) ->
	{ok, []};

%% -----------------------------------------------------------------
%% 28007 将所有物品放入背包
%% -----------------------------------------------------------------
read(28007, _R) ->
	{ok, []};

%% -----------------------------------------------------------------
%% 28008 获取仓库的容量
%% -----------------------------------------------------------------
read(28008, _R) ->
	{ok, []};

%% -----------------------------------------------------------------
%% 错误处理
%% -----------------------------------------------------------------
read(_Cmd, _R) ->
    {error, no_match}.

%%%=========================================================================
%%% 组包函数
%%%=========================================================================
%% -----------------------------------------------------------------
%% 2800 1获取全服开宝箱记录
%% -----------------------------------------------------------------
write(28001, [Len, LogsList]) ->
	Data = <<Len:16, LogsList/binary>>,
	{ok, pt:pack(28001, Data)};

%% -----------------------------------------------------------------
%% 28002 开宝箱
%% -----------------------------------------------------------------
%% write(28002, [Result, HoleType, Len, GoodsList]) ->
%% 	Data = <<Result:16, HoleType:16, Len:16, GoodsList/binary>>,
%% 	{ok, pt:pack(28002, Data)};
write(28002, [Result, HoleType, GoodsListInit]) ->
	GoodsList = lists:map(fun pack_box_goods_result/1, GoodsListInit),
	GoodsListBin = tool:to_binary(GoodsList),
	Len = length(GoodsList),
	Data = <<Result:16, HoleType:16, Len:16, GoodsListBin/binary>>,
	{ok, pt:pack(28002, Data)};
%% -----------------------------------------------------------------
%% 28003 获取诛邪仓库数据
%% -----------------------------------------------------------------
write(28003, [GoodsListInit]) ->
	GoodsList = lists:map(fun handle_warehouse/1, GoodsListInit),
	Len = length(GoodsList),
	GoodsListBin = tool:to_binary(GoodsList),
	Data = <<Len:16, GoodsListBin/binary>>,
	{ok, pt:pack(28003, Data)};

%% -----------------------------------------------------------------
%% 28004 将物品放入背包
%% -----------------------------------------------------------------
write(28004, [Result, GoodsIndex]) ->
	Data = <<Result:16, GoodsIndex:16>>,
	{ok, pt:pack(28004, Data)};

%% -----------------------------------------------------------------
%% 28005 将物品丢弃
%% -----------------------------------------------------------------
write(28005, [Result]) ->
	Data = <<Result:16>>,
	{ok, pt:pack(28005, Data)};

%% -----------------------------------------------------------------
%% 28006 丢弃仓库中的所有物品
%% -----------------------------------------------------------------
write(28006, [Result]) ->
	Data = <<Result:16>>,
	{ok, pt:pack(28006, Data)};

%% -----------------------------------------------------------------
%% 28007 将所有物品放入背包
%% -----------------------------------------------------------------
write(28007, [Result]) ->
	Data = <<Result:16>>,
	{ok, pt:pack(28007, Data)};

%% -----------------------------------------------------------------
%% 28008 获取仓库的容量
%% -----------------------------------------------------------------
write(28008, [RemainCells]) ->
	Data = <<RemainCells:16>>,
	{ok, pt:pack(28008, Data)};

%% -----------------------------------------------------------------
%% 错误处理
%% -----------------------------------------------------------------
write(Cmd, _R) ->
?INFO_MSG("~s_errorcmd_[~p] ",[misc:time_format(yg_timer:now()), Cmd]),
    {ok, pt:pack(0, <<>>)}.


%%
%% Local Functions
%%
pack_box_goods_result(GoodsElem) ->
	{Gid, GoodsNum, BaseGoodsId, GoodsName} = GoodsElem,
	GoodsNameBin = tool:to_binary(GoodsName),
	Len = byte_size(GoodsNameBin),
	<<Gid:32, GoodsNum:16,BaseGoodsId:32,Len:16,GoodsNameBin/binary>>.

handle_warehouse(BoxGoods) ->
	#goods{id = Gid,
		   num = GoodsNum,
		   goods_id = BaseGoodsId} = BoxGoods,
	GoodsNameBin = tool:to_binary([]),
	Len = byte_size(GoodsNameBin),
	<<Gid:32, GoodsNum:16, BaseGoodsId:32,Len:16, GoodsNameBin/binary>>.
